'''
Created on 2011-10-30

@author: tony.li.fly@gmail.com
'''
from xyh.oa.models import Feetype,Currency
from django.http import HttpResponse
from django.utils import simplejson as json
def listall(request):
    result = []
    for c in Feetype.objects.order_by('code').all():
        result.append({'id':c.id, 'code':c.code, 'name':c.name,'default_currencytype_id':c.default_currencytype.id,'default_currencytype_code':c.default_currencytype.code})
    return HttpResponse(json.dumps(result))


def delFeetype(request, feetype_id):
    feetype = Feetype.objects.get(pk=feetype_id)
    feetype.delete()
    return HttpResponse()
    
def addFeetype(request):
    feetype = Feetype(code=request.POST['code'], name=request.POST['name'],default_currencytype = Currency.objects.get(pk=request.POST['default_currencytype_id']))
    feetype.save()
    return HttpResponse()

def updateFeetype(request):
    feetype = Feetype.objects.get(pk=request.POST['id'])
    feetype.name = request.POST['name']
    feetype.code = request.POST['code']
    feetype.default_currencytype = Currency.objects.get(pk=request.POST['default_currencytype_id'])
    feetype.save()
    return HttpResponse()
